package com.ws.controller;

import com.ws.common.PaasPage;
import com.ws.common.PaasResponse;
import com.ws.entity.OrderItem;
import com.ws.response.OrderPageResponse;
import com.ws.response.OrderResponse;
import com.ws.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @author ：ws
 * @date ：Created in 2023/10/24 12:29
 * @description：TODO
 * @modified By：
 * @version: 1.0.0
 */
@Controller
@RefreshScope
@CrossOrigin
@RequestMapping("/order")
public class OrderController {
    @Autowired
    OrderService orderService;

    /**
     * 订单查询接口
     * @param pageable
     * @param bookId
     * @return
     */
    @GetMapping("")
    public ResponseEntity<OrderPageResponse> getOrderPage(
            @PageableDefault(page = 1, direction = Sort.Direction.DESC) Pageable pageable,
            @RequestParam(value="bookId",required=false) Long bookId,
            @RequestParam(value="statusCd",required=false) String statusCd
    ){
        PaasPage<OrderItem> orderPaasPage = orderService.getOrderPage(pageable,bookId,statusCd);
        return ResponseEntity.ok(new OrderPageResponse(orderPaasPage));
    }

    /**
     * 订单新增接口
     * @param orderItem
     * @return
     */
    @PostMapping("")
    public ResponseEntity<OrderResponse> saveOrder(@RequestBody @Valid OrderItem orderItem){
        OrderItem orderItem1 = orderService.saveOrder(orderItem);
        return ResponseEntity.ok(new OrderResponse(orderItem1));
    }

    /**
     * 查询对应订单的基本信息
     * @param orderId
     * @return
     */
    @GetMapping("/{orderId}/basicInfo")
    public ResponseEntity<OrderResponse> getOrderBasicInfo(@PathVariable Long orderId){
        OrderItem orderItem = orderService.getOrderBasicInfo(orderId);
        return ResponseEntity.ok(new OrderResponse(orderItem));
    }

    /**
     * 删除对应的订单
     * @param orderId
     * @return
     */
    @PostMapping("/d/{orderId}")
    public ResponseEntity delOrder(@PathVariable Long orderId)  {
        orderService.delOrder(orderId);
        return ResponseEntity.ok(new PaasResponse());
    }

    /**
     * 修改对应的订单
     * @param orderId
     * @return
     */
    @PostMapping("/u/{orderId}")
    public ResponseEntity<OrderResponse> updateOrder(@PathVariable Long orderId,@RequestBody OrderItem orderItem)  {
        OrderItem orderItem1 = orderService.updateOrder(orderId, orderItem);
        return ResponseEntity.ok(new OrderResponse(orderItem1));
    }
}
